﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace WeChatDataSrc
{
    class RefineForeData
    {
        static string URL = ConfigurationSettings.AppSettings["RefineForeURL"];
        /// <summary>
        /// 上传数据
        /// </summary>
        public static void UploadData(DateTime? sd)
        {
            DateTime NowMaxDateTime;
            string MaxDateSendpoint = ReadMax.ReadMaxRefine();
            string SendPoint = string.Empty;
            if (MaxDateSendpoint != "none")
            {
                SendPoint = MaxDateSendpoint.Split('|')[1];
                if (DateTime.TryParse(MaxDateSendpoint.Split('|')[0], out NowMaxDateTime) == false)
                {
                    NowMaxDateTime = DateTime.Now;
                }
                else
                {
                    if (SendPoint == "H16")
                    {
                        NowMaxDateTime = NowMaxDateTime.AddDays(1);
                    }
                }
            }
            else
            {
                NowMaxDateTime = DateTime.Now;
            }

            if (sd.HasValue)
            {
                NowMaxDateTime = Convert.ToDateTime(sd);
            }
            DateTime BeginDate = NowMaxDateTime;
            GetData(BeginDate);
        }

        private static void GetData(DateTime Time)
        {
            DateTime Now = DateTime.Now;
            
            while (true)
            {
                if (DateTime.Compare(Time, Now) > 0)
                {
                    break;
                }
                StringBuilder sb = new StringBuilder(1024 * 15);
                string Result = string.Format("{0}^", Time.ToString("yyyy-MM-dd"));
                sb.Append(Result);
                string ReSendPoint = string.Empty;
                string[] Spans = new string[] 
                { "FD00_03", "FD03_06", "FD06_09", "FD09_12", "FD12_15", 
                    "FD15_18", "FD18_21", "FD21_24", "FD24_27", "FD27_30", 
                    "FD30_33", "FD33_36", "FD36_39", "FD39_42", "FD42_45", 
                    "FD45_48", "FD48_51", "FD51_54", "FD54_57", "FD57_60", 
                    "FD60_63", "FD63_66", "FD66_69", "FD69_72", "FD72_78", 
                    "FD78_84", "FD84_90", "FD90_96", "FD96_102", "FD102_108", 
                    "FD108_114", "FD114_120", "FD120_126", "FD126_132", 
                    "FD132_138", "FD138_144", "FD144_150", "FD150_156", 
                    "FD156_162" ,"FD00_12", "FD12_24", "FD24_36", "FD36_48", 
                    "FD48_60" ,"FD60_72", "FD72_84", "FD84_96", "FD96_108", 
                    "FD108_120" ,"FD120_132", "FD132_144", "FD144_156", "FD156_168" 

                    //下面一行是测试数据
                    ,"FD00_12", "FD00_24", "FD108_120", "FD12_24", "FD120_132", 
                };
                foreach (string span in Spans)
                {
                    string sql = string.Format("select * from Detailed_Fore where [date]='{0}' and span='{1}' and semitype='Auto' order by sendpoint desc", Time.ToString("yyyyMMdd"), span);
                    DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.RefineFore_ConnectionString, CommandType.Text, sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {                        
                        List<String> FilteredTypes = new List<string>();
                        foreach (DataRow item in dt.Rows)
                        {
                            String Type = item["type"].ToString();
                            String SendPoint = item["sendpoint"].ToString();
                            ReSendPoint = SendPoint;
                            if (FilteredTypes.Contains(Type))
                            {
                                continue;
                            }
                            else //没有添加过该种类型
                            {
                                String Value = item["value"].ToString();
                                var t = string.Format("{0}:{1}:{2}^", span ,Type, Value);
                                sb.Append(t);
                                FilteredTypes.Add(Type);
                            }
                        }
                        
                    }
                }
                Time = Time.AddDays(1);
                sb.Append(ReSendPoint);
                if (sb.Length>11)
                {
                    HttpPost.SendData(URL, "data=" + sb.ToString().Replace(";","*"));
                }
            }
        }

        private class CityForeStationModel
        {
            public CityForeStationModel()
            {
                this.StationCode = "";
                this.Data = new CityForeDataModel();
            }
            public string StationCode { get; set; }
            public string Date { get; set; }
            public string Span { get; set; }
            public string SendPoint { get; set; }
            public CityForeDataModel Data { get; set; }
        }

        private class CityForeDataModel
        {
            public CityForeDataModel()
            {
                this.Maxt = "";
                this.Mint = "";
                this.WindDirection = "";
                this.WindSpeed = "";
                this.Rain = "";
            }
            public string Maxt { get; set; }
            public string Mint { get; set; }
            public string WindDirection { get; set; }
            public string WindSpeed { get; set; }
            public string Rain { get; set; }
            public string Curt { get; set; }
            

        }
    }
}
